﻿
/*
*	与后台有关的点击事件
*/

$(document).ready(function () {
    click_bottom_reg();
    click_bottom_login();
    click_reg();               //注册
    click_login();             //登录
    click_findpsw();           //找回密码
    btn_autologin();        //自动登录单选框
});

//////////////////////////////////////////////////////////////////////////
/// 数据
var isautologin = false;
var page = "login";

///自动登录单选框
function btn_autologin() {
    $("#a_checkbox").bind("click", function () {
        if ($("#checkbox_remember").attr("checked")) {    //自动登录
            $("#a_checkbox").css("background-position", "0 -300px");
            $("#checkbox_remember").attr("checked", false);
            isautologin = false;
        }
        else {
            $("#a_checkbox").css("background-position", "-20 -300px");
            $("#checkbox_remember").attr("checked", true);
          isautologin = true;
        }
    });
}


///登录
function click_bottom_login() {
    $("#bottom_btn_login").click(function (event) {
        location = '#div_login';
        page = "login";
    });
}
///注册
function click_bottom_reg() {
    $("#bottom_btn_reg").click(function (event) {
        location = '#div_register';
        page = "register";
    });
}


///登录
function click_login() {
    $("#btn_login").click(function (event) {
        login();
    });
}

/// 注册
function click_reg() {
    $("#btn_register").click(function (event) {
        reg();
    });
}

//找回密码
function click_findpsw() {

   
}


///回车
$(document).keydown(function (event) {
    if (event.which == 13)       //13等于回车键(Enter)键值,ctrlKey 等于 Ctrl
    {
        if (page == "login") {
            login();
        }
        else {
            reg();

        }
    }
});


function login() {
    if (!Is_All_login) {   //邮箱不正确
        $("#text_username").focus();
        return;
    }
    if ($("#text_userpsw").val() == '') {  //密码框为空
        $("#text_userpsw").focus();
        return;
    }
    $.ajax({
        url: "/Mange_State/Login",
        type: "post",
        cache: false,
        data: { Email: $("#text_username").val(), Psw: $("#text_userpsw").val(), AutoLogin: isautologin },
        dataType: "json",
        success: function (msg) {
            if (msg.Result) {    //登录成功
                location = "/View_Main/Main?name=" + $("#text_username").val();  //跳转到主页 
            }
            else {
                alert(msg.Message);
            }
        }
    });
}

function reg() {
    var username = $("#input_reg_username").val();
    var userpsw = $("#input_reg_userpsw").val();
    if (Is_All) {    ///所有表单正确
        $.ajax({
            url: "/Mange_State/Register",
            type: "post",
            cache: false,
            data: { Email: username, Psw: userpsw },
            dataType: "json",
            success: function (msg) {
                if (msg.Result) {
                    alert(msg.Message);
                    page = "login";
                    location = '#div_login';
                    $("#text_username").val(username);
                    $("#text_userpsw").val(userpsw);
                }
                else {
                    alert(msg.Message);
                }
            }
        });
    }
}